package com.turing.post.cardmanagement.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.cardmanagement.util.ApduExecuteObj;
import com.turing.post.cardmanagement.util.AppCardApduUtil;
import com.turing.post.cardmanagement.util.AppManagement;
import com.turing.post.cardmanagement.util.AppManagementBusiness;
import com.turing.post.cardmanagement.util.AppManagementDao;
import com.turing.post.cardmanagement.util.AppZoneInf;
import com.turing.post.cardmanagement.util.CardApdu;
import com.turing.post.cardmanagement.util.CardIssueRoot;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 应用管理信息
 * 
 * @author zxp
 * @Apr 9, 2013
 */
@Stateless(mappedName = "cardmanagement.impl.AppManagementBusinessImpl")
@Remote
@Local
public class AppManagementBusinessImpl implements AppManagementBusiness {

	@EJB(mappedName = "cardmanagement.impl.AppManagementDaoImpl")
	private AppManagementDao appManagementDaoImpl;

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	/**
	 * 异常抛出方法
	 * 
	 * @param error
	 * @throws PCPSApplicationException
	 */
	private void errException(String error) throws PCPSApplicationException {
		throw new PCPSApplicationException(
				PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
				error);
	}

	@Override
	public void insertAppManagement(PCPSWebInput input, AppManagement amg)
			throws PCPSApplicationException {
		/**
		 * 参数合法性检查
		 */
		if (input.getOperId() == null) {
			errException("登录用户为空!");
		}
		if (amg == null) {
			errException("应用管理信息为空!");
		}

		if (PCPSUtil.isEmpty(amg.getAID())
				|| amg.getAID().trim().length() != 18) {
			errException("应用标识ID为空或长度不为18位!");
		}

		if (PCPSUtil.isEmpty(amg.getAppName())) {
			errException("应用名称为空!");
		}

		if (PCPSUtil.isEmpty(amg.getActivate())) {
			errException("激活状态为空!");
		}

		if (appManagementDaoImpl.getAppManagementByAID(amg.getAID()) != null) {
			errException("应用标识ID已存在!");
		}
		amg.setIssueCompanyNo(PCPSUtil.getIssueCompanyNo(input));
		amg.setAppId(commonDaoImpl.getUniqueIntegerKey());
		appManagementDaoImpl.insert(amg);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(input, SysOperLog.operType_Add,
				"新增 应用管理信息", "AppName:" + amg.getAppName());
	}

	@Override
	public void updateAppManagement(PCPSWebInput input, AppManagement amg)
			throws PCPSApplicationException {
		/**
		 * 参数合法性检查
		 */
		if (input.getOperId() == null) {
			errException("登录用户为空!");
		}
		if (amg == null) {
			errException("应用管理信息为空!");
		}

		if (amg.getAppId() == null) {
			errException("应用管理编号为空!");
		}

		if (PCPSUtil.isEmpty(amg.getAID())
				|| amg.getAID().trim().length() != 18) {
			errException("应用标识ID为空或长度不为18位!");
		}

		if (PCPSUtil.isEmpty(amg.getAppName())) {
			errException("应用名称为空!");
		}

		if (PCPSUtil.isEmpty(amg.getActivate())) {
			errException("激活状态为空!");
		}
		AppManagement updateAmg = appManagementDaoImpl
				.getAppManagementByAppId(amg.getAppId());
		if (updateAmg == null) {
			errException("应用管理编号错误，系统中不存在该编号!");
		}
		if (!updateAmg.getAID().equals(amg.getAID())) {
			if (appManagementDaoImpl.getAppManagementByAID(amg.getAID()) != null) {
				errException("应用标识ID:" + amg.getAID() + "已存在!");
			}
			updateAmg.setAID(amg.getAID());
		}
		updateAmg.setJndiName(amg.getJndiName());
		updateAmg.setAppName(amg.getAppName());
		updateAmg.setServiceAddr(amg.getServiceAddr());
		updateAmg.setRemark(amg.getRemark());
		updateAmg.setActivate(amg.getActivate());
		appManagementDaoImpl.update(updateAmg);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(input, SysOperLog.operType_Edit,
				"修改 应用管理信息", "AppName:" + amg.getAppName());
	}

	public ApduExecuteObj generateReadCardApdu() {
		ApduExecuteObj aeo = new ApduExecuteObj();

		List<CardApdu> apdulist = new ArrayList<CardApdu>();

		CardApdu apdu = new CardApdu();

		// step 1 -- OPEN设备的APDU指令
		AppCardApduUtil.openDevice(apdulist, "");

		// step 2 -- 产生打开MF文件APDU指令
		apdu = new CardApdu();
		apdu
				.setApduCategory(CardApdu.APDU_ERR_STOP
						| CardApdu.APDU_SELECT_FILE);
		apdu.setApdu(AppCardApduUtil.getApduSelectMF300());
		apdulist.add(apdu);

		// step 3 -- 产生读卡片发行APDU指令(MF/0005)
		apdu = new CardApdu();
		apdu
				.setApduCategory(CardApdu.APDU_ERR_STOP
						| CardApdu.APDU_READ_BINARY);
		apdu.setApdu(AppCardApduUtil.getApduReadBinary(0x05, true, 0, 0));
		apdulist.add(apdu);

		// step 4 -- CLOSE设备的APDU指令
		AppCardApduUtil.closeDevice(apdulist);

		aeo.setApduList(apdulist);
		return aeo;
	}

	@Override
	public AppZoneInf parseReadCardApdu(ApduExecuteObj aeo)
			throws PCPSApplicationException {
		// 检查指令执行结果的合法性
		List<CardApdu> apdulist = aeo.getApduList();
		if (apdulist == null || apdulist.size() != 4)
			throw new PCPSApplicationException("", "无效的APDU执行结果列表!");

		int idx = 0;

		AppZoneInf azi = new AppZoneInf();

		// step 1. 检查OPEN情况
		String result = AppCardApduUtil.getExpectedApdu(apdulist, idx++,
				CardApdu.APDU_OPEN, "寻卡失败!");
		result = result.substring(0, result.length() - 4);
		azi.setFullATS(result);
		azi.setATS(AppCardApduUtil.parseATS(result));

		// step 2. 检查根目录存折情况
		result = AppCardApduUtil.getExpectedApdu(apdulist, idx++,
				CardApdu.APDU_SELECT_FILE, "卡片打开失败!");

		// step 3. 检查读卡片发行区情况
		result = AppCardApduUtil.getExpectedApdu(apdulist, idx++,
				CardApdu.APDU_READ_BINARY, "读卡片发行区失败!");
		azi.setCardIssue(CardIssueRoot.parseCardIssueRoot(result.substring(0,
				result.length() - 4)));

		return azi;
	}
}
